package cn.wsn2018.tools.service;

import cn.wsn2018.tools.common.CommonValue;
import cn.wsn2018.tools.dao.EncodeDesignDao;
import cn.wsn2018.tools.model.Design.EncodeDesign;
import cn.wsn2018.tools.util.JsonUtil;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 类名称:EncodeDesignService
 * 类描述:TODO
 * 创建人:nzx
 * 创建时间:2019/7/27 20:09
 */
@Service
public class EncodeDesignService {
    private static final Logger logger = LoggerFactory.getLogger(EncodeDesignService.class);
    @Autowired
    private EncodeDesignDao encodeDesignDao;

    /**
     * 插入功能编码
     * @param encodeDesign
     * @return
     */
    public String dealEncodeDesignSave(EncodeDesign encodeDesign){
        if(encodeDesign!=null){
            String id = encodeDesignDao.saveEncodeDesign(encodeDesign);
            if(id!=null){
                JSONObject objectId = new JSONObject();
                objectId.put("id",id);
                return JsonUtil.successJson(CommonValue.JSON_INSERT_SUCCESS,objectId,null);
            }else {
                return JsonUtil.failedJson(CommonValue.JSON_INSERT_FAILED);
            }
        }else {
            return JsonUtil.failedJson(CommonValue.JSON_INPUT_ERROR);
        }

    }

    /**
     * 查询功能编码详情
     * @param designId
     * @return
     */
    public String dealEncodeDesignQueryByDesignId(String designId){
        if(designId!=null&&!designId.isEmpty()){
            EncodeDesign encodeDesign = encodeDesignDao.getEncodeDesignByDesignId(designId);
            JSONObject jsonObject = JSONObject.fromObject(encodeDesign);
            return JsonUtil.successJson(CommonValue.JSON_QUERY_SUCCESS,jsonObject,null);
        }else {
            return JsonUtil.failedJson(CommonValue.JSON_INPUT_ERROR);
        }
    }

    /**
     * 修改功能编码
     * @param encodeDesign
     * @return
     */
    public String dealEncodeDesignUpdate(EncodeDesign encodeDesign){
        if(encodeDesign!=null&&encodeDesign.getDesignId()!=null){
            boolean result = encodeDesignDao.updateEncodeDesign(encodeDesign);
            if(result){
                return JsonUtil.successJson(CommonValue.JSON_UPDATE_SUCCESS,null,null);
            }else {
                return JsonUtil.failedJson(CommonValue.JSON_UPDATE_FAILED);
            }
        }else {
            return JsonUtil.failedJson(CommonValue.JSON_INPUT_ERROR);
        }
    }

    /**
     * 删除功能编码
     * @param designId
     * @return
     */
    public String dealEncodeDesignDelete(String designId){
        if(designId!=null&&!designId.isEmpty()){
            boolean result = encodeDesignDao.deleteEncodeDesign(designId);
            if(result){
                return JsonUtil.successJson(CommonValue.JSON_DELETE_SUCCESS, null,null);
            }else {
                return JsonUtil.failedJson(CommonValue.JSON_DELETE_FAILED);
            }
        }else {
            return JsonUtil.failedJson(CommonValue.JSON_INPUT_ERROR);
        }
    }
}
